package controle;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modelo.dao.ContatoDAO;
import modelo.dao.UsuarioDAO;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ListarAction extends Action{
	static Logger logger = Logger.getLogger(ListarAction.class);
	private List contatos;
	
	@Override
	public ActionForward execute(
				ActionMapping mapping, 
				ActionForm form,
				HttpServletRequest request, 
				HttpServletResponse response)
			throws Exception {
		
		ContatoDAO dao = new ContatoDAO();
		HttpSession session = request.getSession(false);
		try{
			
			setContatos(dao.listar(session.getAttribute("usuario").toString()));
			System.out.println("Lista de contatos adicionados: "+getContatos());
			request.setAttribute("contatos",this.getContatos());
			return mapping.findForward("sucesso");
			
		}catch (Exception e) {
			return mapping.findForward("erro");
		}
		
		
	}

	public List getContatos() {
		return contatos;
	}

	public void setContatos(List contatos) {
		this.contatos = contatos;
	}

}
